//
//  ChartsViewController.m
//  ZhuKuang
//
//  Created by mafy on 17/7/14.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#define screenWidth  [[UIScreen mainScreen] bounds].size.width
#define screenHeight [[UIScreen mainScreen] bounds].size.height

#define kNavigationViewHeight 64
#define kToolsViewHeight 44

#import "ChartsViewController.h"
#import "FCCustomIolAndCurveButton.h"
#import "FCVehicleIolAndCurveOptionTableView.h"
#import "SymbolsValueFormatter.h"
#import "DateValueFormatter.h"
#import "SetValueFormatter.h"
#import "CurveListModel.h"
#import "CarCurveModel.h"

@interface ChartsViewController ()<ChartViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic,strong) LineChartView * lineView;

@property (nonatomic,strong) UILabel * markY;

@property (nonatomic, strong) NSMutableArray *speedCurveListArray;//速度曲线数据源

@property (nonatomic,strong) NSMutableArray *carCurveListArray;//油量曲线数据源

@property (nonatomic,assign) CGFloat speedLimitLineValue;//速度警戒线

@property (nonatomic,assign) CGFloat oilLimitLineAValue;//速度警戒线

@property (nonatomic,assign) CGFloat oilLimitLineBValue;//速度警戒线

@property (nonatomic,strong) ChartYAxis *leftAxis;//左边Y轴线

@property (nonatomic, strong) UIView *navigationView;//自定义导航视图

//工具条
@property (nonatomic, strong) UIView *toolsView;
@property (nonatomic, strong) FCCustomIolAndCurveButton *selectedBtn;
@property (nonatomic, strong) UILabel *nameLabel;//显示车辆名称
@property (nonatomic, strong) UIView *promptView;//A杆 B杆 View

//下拉选择框
@property (nonatomic, strong) NSArray *selectedArray;
@property (nonatomic, strong) UIView *selectedBackView;
@property (nonatomic, strong) FCVehicleIolAndCurveOptionTableView *optionsTableView;

@end

@implementation ChartsViewController


- (BOOL)shouldAutorotate{
    
    return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
     
 
     return UIInterfaceOrientationMaskLandscape;
 }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    
    return UIInterfaceOrientationLandscapeRight;
}


- (UILabel *)markY{
    
    if (!_markY) {
        
        _markY = [[UILabel alloc]initWithFrame:CGRectMake(-5, 0, 35, 25)];
        _markY.font = [UIFont systemFontOfSize:9.0];
        _markY.textAlignment = NSTextAlignmentCenter;
        _markY.text = @"";
        _markY.textColor = [UIColor blackColor];
        _markY.backgroundColor = [UIColor clearColor];
    }
    return _markY;
}

- (LineChartView *)lineView {
    if (!_lineView) {
        _lineView = [[LineChartView alloc] initWithFrame:CGRectMake(10, kNavigationViewHeight+kToolsViewHeight, screenWidth - 10, screenHeight-kNavigationViewHeight-kToolsViewHeight - 10)];
        _lineView.delegate = self;//设置代理
        _lineView.backgroundColor =  [UIColor whiteColor];
        _lineView.noDataText = @"暂无数据";
        _lineView.chartDescription.enabled = YES;
        _lineView.scaleYEnabled = YES;//Y轴缩放
        _lineView.doubleTapToZoomEnabled = NO;//取消双击缩放
        _lineView.dragEnabled = YES;//启用拖拽图标
        _lineView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
        _lineView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1)，数值越小，惯性越不明显
        //设置滑动时候标签
        ChartMarkerView *markerY = [[ChartMarkerView alloc]
                                    init];
        markerY.offset = CGPointMake(-999, -8);
        markerY.chartView = _lineView;
        _lineView.marker = markerY;
        [markerY addSubview:self.markY];
        
        
        _lineView.rightAxis.enabled = NO;//不绘制右边轴
        self.leftAxis = _lineView.leftAxis;//获取左边Y轴
        self.leftAxis.labelCount = 3;//Y轴label数量，数值不一定，如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
        self.leftAxis.forceLabelsEnabled = YES;//不强制绘制指定数量的label
        self.leftAxis.axisMinValue = 0;//设置Y轴的最小值
        self.leftAxis.axisMaxValue = 60;//设置Y轴的最大值
        self.leftAxis.inverted = NO;//是否将Y轴进行上下翻转
        self.leftAxis.axisLineColor = [UIColor colorWithHexString:@"#1981C6"];//Y轴颜色
        self.leftAxis.valueFormatter = [[SymbolsValueFormatter alloc]init];
        self.leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
        self.leftAxis.labelTextColor = [UIColor colorWithHexString:@"#1981C6"];//文字颜色
        self.leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字体
        self.leftAxis.gridColor = [UIColor grayColor];//网格线颜色
        self.leftAxis.gridAntialiasEnabled = NO;//开启抗锯齿
      
        
        
        
        ChartXAxis *xAxis = _lineView.xAxis;
        xAxis.granularityEnabled = YES;//设置重复的值不显示
        xAxis.labelPosition = XAxisLabelPositionBottom;//设置x轴数据在底部
        xAxis.gridColor = [UIColor clearColor];
        xAxis.axisLineColor = [UIColor colorWithHexString:@"#1981C6"];
        xAxis.labelTextColor = [UIColor colorWithHexString:@"#1981C6"];//文字颜色
     
        _lineView.maxVisibleCount = 999;//
        
        //描述及图例样式
        [_lineView setDescriptionText:@""];
        _lineView.legend.enabled = NO;
        
        [_lineView animateWithXAxisDuration:1.0f];
    }
    return _lineView;
}
- (void)createChartLimitLineForOilCurve :(ChartYAxis *)leftAxis {
    
    ChartLimitLine *limitLineA = [[ChartLimitLine alloc] initWithLimit:self.oilLimitLineAValue label:@""];
    limitLineA.lineWidth = 1;
    limitLineA.lineColor = [UIColor colorWithHexString:@"#797979"];
    limitLineA.lineDashLengths = @[@5.0f,@5.0f];

    limitLineA.labelPosition = ChartLimitLabelPositionRightTop;
    [leftAxis addLimitLine:limitLineA];
    leftAxis.drawLimitLinesBehindDataEnabled = YES;
    
    ChartLimitLine *limitLineB = [[ChartLimitLine alloc] initWithLimit:self.oilLimitLineBValue label:@""];
    limitLineB.lineWidth = 1;
    limitLineB.lineColor = [UIColor colorWithHexString:@"#797979"];
    limitLineB.lineDashLengths = @[@5.0f,@5.0f];
    limitLineB.labelPosition = ChartLimitLabelPositionRightTop;
    [leftAxis addLimitLine:limitLineB];
    leftAxis.drawLimitLinesBehindDataEnabled = YES;
}

- (void)createChartLimitLineForSpeedCurve :(ChartYAxis *)leftAxis {
    
    ChartLimitLine *limitLineA = [[ChartLimitLine alloc] initWithLimit:30 label:@"30"];
    limitLineA.lineWidth = 1;
    limitLineA.lineColor = [UIColor colorWithHexString:@"#797979"];
    limitLineA.lineDashLengths = @[@5.0f,@5.0f];
    limitLineA.labelPosition = ChartLimitLabelPositionRightTop;
    [leftAxis addLimitLine:limitLineA];
    leftAxis.drawLimitLinesBehindDataEnabled = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"曲线图";
 
   
    if (0 == self.curveType) {
        [self drawCarOilCurve];
        [self.selectedBtn setTitle:@"油耗曲线" forState:UIControlStateNormal];
        self.promptView.hidden = NO;
    }else {
        [self drawSpeedCurve];
        [self.selectedBtn setTitle:@"速度曲线" forState:UIControlStateNormal];
        self.promptView.hidden = YES;
    }
}
//绘制速度曲线
- (void)drawSpeedCurve {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.leftAxis.axisMaxValue = 60;
        [self getSpeedCurveWithCarID:nil andStartTime:nil andEndTime:nil andBusinessID:nil];
        
    });
}
//绘制油耗曲线
- (void)drawCarOilCurve {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
        [self getCarCurveWithCarID:nil andStartTime:nil andEndTime:nil andBusinessID:nil];
        
    });

}
- (LineChartData *)setData{

    NSInteger xVals_count = self.speedCurveListArray.count;//X轴上要显示多少条数据
    //X轴上面需要显示的数据
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < xVals_count; i++) {

        CurveListModel *model = self.speedCurveListArray[i];
        NSString *xValue = [model.ServerTime substringWithRange:NSMakeRange(11, 5)];
        
        [xVals addObject:xValue];
    }
    
    _lineView.xAxis.valueFormatter = [[DateValueFormatter alloc]initWithArr:xVals];
    
    //对应Y轴上面需要显示的数据
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
  
    for (int i = 0; i < xVals_count; i++) {

        CurveListModel *model = self.speedCurveListArray[i];
        ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:i y:[model.Speed floatValue]];
        
        [yVals addObject:entry];
        
    }
    
    
    
    LineChartDataSet *set1 = nil;
    
   
    
    if (_lineView.data.dataSetCount > 0) {
        
        LineChartData *data = (LineChartData *)_lineView.data;
        set1 = (LineChartDataSet *)data.dataSets[0];
        set1.values = yVals;
        
        set1.valueFormatter = [[SetValueFormatter alloc]initWithArr:yVals];
        return data;
        
    }else{
        //创建LineChartDataSet对象
        set1 = [[LineChartDataSet alloc]initWithValues:yVals label:nil];
        //设置折线的样式
        set1.lineWidth = 2.0/[UIScreen mainScreen].scale;//折线宽度
        
        set1.drawValuesEnabled = YES;//是否在拐点处显示数据
        set1.valueFormatter = [[SetValueFormatter alloc]initWithArr:yVals];
        
        set1.valueColors = @[[UIColor brownColor]];//折线拐点处显示数据的颜色
        
        [set1 setColor:[UIColor colorWithHexString:@"#18A3D1"]];//折线颜色
       
        set1.drawSteppedEnabled = NO;//是否开启绘制阶梯样式的折线图
        //折线拐点样式
        set1.drawCirclesEnabled = NO;//是否绘制拐点
        set1.drawFilledEnabled = NO;//是否填充颜色
        
        //将 LineChartDataSet 对象放入数组中
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        
        //创建 LineChartData 对象, 此对象就是lineChartView需要最终数据对象
        LineChartData *data = [[LineChartData alloc]initWithDataSets:dataSets];
        
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];//文字字体
        [data setValueTextColor:[UIColor blackColor]];//文字颜色
        
        
        return data;
    }
    
}
 //设置油耗曲线数据
- (LineChartData *)setDataForCarCurve {
    
    NSInteger xVals_count = self.carCurveListArray.count;//X轴上要显示多少条数据
    //X轴上面需要显示的数据
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < xVals_count; i++) {
        
        CarCurveModel *model = self.carCurveListArray[i];
        
        NSString *xValue = [model.ServerTime substringWithRange:NSMakeRange(11, 5)];
        
        [xVals addObject:xValue];
    }
    
    _lineView.xAxis.valueFormatter = [[DateValueFormatter alloc]initWithArr:xVals];
    
    //对应Y轴上面需要显示的数据
    NSMutableArray *yAVauleArray = [[NSMutableArray alloc] init];
    NSMutableArray *yBVauleArray = [[NSMutableArray alloc] init];
    
    
    for (int i = 0; i < xVals_count; i++) {
    
        CarCurveModel *model = self.carCurveListArray[i];
        
        CGFloat yAvalue = [[NSString stringWithFormat:@"%.1f",[model.OilNumA floatValue]] floatValue];
        CGFloat yBvalue = [[NSString stringWithFormat:@"%.1f",[model.OilNumB floatValue]] floatValue];
        
        ChartDataEntry *entryA = [[ChartDataEntry alloc] initWithX:i y:yAvalue];
        ChartDataEntry *entryB= [[ChartDataEntry alloc] initWithX:i y:yBvalue];
        

        [yAVauleArray addObject:entryA];
        [yBVauleArray addObject:entryB];
        
    }
    
    NSArray *arrayA = [yAVauleArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        ChartDataEntry *firstModel = obj1;
        ChartDataEntry *sencondModel = obj2;
        
        BOOL iSGreater = firstModel.y > sencondModel.y;
        
        if (iSGreater) {
            return NSOrderedDescending;
        }else {
            
            return NSOrderedDescending;
        }
        
    }];
    
    ChartDataEntry *modelA1 = arrayA[0];
    ChartDataEntry *modelA2 = arrayA[self.carCurveListArray.count - 1];
    
    self.oilLimitLineAValue = modelA1.y > modelA2.y?modelA1.y:modelA2.y;

    NSArray *arrayB = [yAVauleArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        ChartDataEntry *firstModel = obj1;
        ChartDataEntry *sencondModel = obj2;
       
        BOOL iSGreater = firstModel.y > sencondModel.y;
        
        if (iSGreater) {
            return NSOrderedDescending;
        }else {
        
            return NSOrderedDescending;
        }

    }];
    
    
    
    ChartDataEntry *modelB = arrayB[0];
    
    self.oilLimitLineBValue = modelB.y;
    
    ChartDataEntry *modelB1 = arrayB[0];
    ChartDataEntry *modelB2 = arrayB[self.carCurveListArray.count - 1];
    
    self.oilLimitLineAValue = modelB1.y > modelB2.y?modelB1.y:modelB2.y;
    
    LineChartDataSet *setA = nil;
    
    LineChartDataSet *setB = nil;
    
    if (_lineView.data.dataSetCount > 0) {
        
        LineChartData *data = (LineChartData *)_lineView.data;
        setA = (LineChartDataSet *)data.dataSets[0];
        setA.values = yAVauleArray;
        setA.valueFormatter = [[SetValueFormatter alloc]initWithArr:yAVauleArray];
        return data;
        
    }else{
        //创建LineChartDataSet对象
        setA = [[LineChartDataSet alloc]initWithValues:yAVauleArray label:@"升"];
        //设置折线的样式
        setA.lineWidth = 2.0/[UIScreen mainScreen].scale;//折线宽度
        
        setA.drawValuesEnabled = NO;//是否在拐点处显示数据
        setA.valueFormatter = [[SetValueFormatter alloc]initWithArr:yAVauleArray];
        
        setA.valueColors = @[[UIColor brownColor]];//折线拐点处显示数据的颜色
        
        [setA setColor:COLOR(24, 134, 200, 1)];//折线颜色
        
        setA.drawSteppedEnabled = NO;//是否开启绘制阶梯样式的折线图
        //折线拐点样式
        setA.drawCirclesEnabled = NO;//是否绘制拐点
        setA.drawFilledEnabled = NO;//是否填充颜色
        
        //将 LineChartDataSet 对象放入数组中
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:setA];
        
        
        setB = [[LineChartDataSet alloc]initWithValues:yBVauleArray label:nil];
        //设置折线的样式
        setB.lineWidth = 2.0/[UIScreen mainScreen].scale;//折线宽度
        
        setB.drawValuesEnabled = NO;//是否在拐点处显示数据
        setB.valueFormatter = [[SetValueFormatter alloc]initWithArr:yAVauleArray];
        
        setB.valueColors = @[[UIColor brownColor]];//折线拐点处显示数据的颜色
        
        [setB setColor:[UIColor greenColor]];//折线颜色
        
        
        setB.drawSteppedEnabled = NO;//是否开启绘制阶梯样式的折线图
        //折线拐点样式
        setB.drawCirclesEnabled = NO;//是否绘制拐点
        setB.drawFilledEnabled = NO;//是否填充颜色
        
        //将 LineChartDataSet 对象放入数组中
        [dataSets addObject:setB];
        
        
        LineChartData *data = [[LineChartData alloc]initWithDataSets:dataSets];
        
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];//文字字体
        [data setValueTextColor:[UIColor blackColor]];//文字颜色
        
        
        return data;
    }


}
- (void)chartValueSelected:(ChartViewBase * _Nonnull)chartView entry:(ChartDataEntry * _Nonnull)entry highlight:(ChartHighlight * _Nonnull)highlight {
    
    _markY.text = [NSString stringWithFormat:@"%.1f",(float)entry.y];
    //将点击的数据滑动到中间
    [_lineView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_lineView.data getDataSetByIndex:highlight.dataSetIndex].axisDependency duration:1.0];
    
    
}
- (void)chartValueNothingSelected:(ChartViewBase * _Nonnull)chartView {
    
    
}

- (void)getSpeedCurveWithCarID:(NSString *)carID andStartTime:(NSString *)starttime andEndTime:(NSString *)endTime andBusinessID:(NSString *)businessID{
    
    __weak typeof(self) weakSelf = self;
    
    NSDictionary *params = @{
                             
                             @"CarID" : @"17",
                             @"starttime" : @"2017-07-14 15:00",
                             @"endtime" : @"2017-07-14 16:00"
                             };
    
    [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
    
    [ClientRequest SOAPMthod:@"getSpeedCurve" soapBody:params success:^(id responseObject) {
        
        [MBProgressHUD hideLoadViewWithView:kWindow];
        
        for (NSDictionary *info in responseObject[@"CurveList"]) {
            
            CurveListModel *model = [CurveListModel mj_objectWithKeyValues:info];
            
            [self.speedCurveListArray addObject:model];
        }
        
        [weakSelf.view addSubview:weakSelf.navigationView];
        [weakSelf.view addSubview:weakSelf.toolsView];
        weakSelf.lineView.data = [weakSelf setData];
        
        [weakSelf.view addSubview:weakSelf.lineView];
        weakSelf.leftAxis.axisMaxValue = 60;
        [weakSelf createChartLimitLineForSpeedCurve:weakSelf.leftAxis];
        
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD postMessageWithText:@"请求失败" View:kWindow];
        
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器的错误原因：%@",str);
    }];
}

- (void)getCarCurveWithCarID:(NSString *)carID andStartTime:(NSString *)starttime andEndTime:(NSString *)endTime andBusinessID:(NSString *)businessID{
    __weak typeof(self) weakSelf = self;

    NSDictionary *params = @{
                             @"CarID" : self.carId,
                             @"starttime" : self.startTime,
                             @"endtime" : self.endTime,
                             
                             };
    [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
    
    [ClientRequest SOAPMthod:@"getCarCurve" soapBody:params success:^(id responseObject) {
        
        [MBProgressHUD hideLoadViewWithView:kWindow];
        
        for (NSDictionary *info in responseObject[@"CurveList"]) {
            
            CarCurveModel *model = [CarCurveModel mj_objectWithKeyValues:info];
            
            [weakSelf.carCurveListArray addObject:model];
        }
        
        [weakSelf.view addSubview:weakSelf.navigationView];
        [weakSelf.view addSubview:weakSelf.toolsView];
        [weakSelf.view addSubview:weakSelf.lineView];
        weakSelf.lineView.data = [weakSelf setDataForCarCurve];
        weakSelf.leftAxis.axisMaxValue = 500;
        [weakSelf createChartLimitLineForOilCurve:weakSelf.leftAxis];
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD postMessageWithText:@"请求失败" View:kWindow];
        
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器的错误原因：%@",str);
    }];
}

#pragma mark - 设置工具条
#pragma mark 选择曲线类型
- (void)selectedButtonClick:(UIButton *)sender{
    
    CGRect rect = [self.toolsView convertRect:sender.frame toView:self.view];
    
    [self showViewWithY:rect.origin.y+34 andHeight:88 andTag:100];
    _optionsTableView.type = @"3";
    _optionsTableView.dataArray = self.selectedArray;
    
   
}

- (UIView *)toolsView{
    
    if (!_toolsView) {
        
        _toolsView = [[UIView alloc]init];
        _toolsView.frame = CGRectMake(0, kNavigationViewHeight, screenWidth, kToolsViewHeight);
        
        //曲线选项按钮
        _selectedBtn = [[FCCustomIolAndCurveButton alloc]init];
        [_selectedBtn setTitle:@"速度曲线" forState:UIControlStateNormal];
        [_selectedBtn addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _selectedBtn.frame = CGRectMake(5, 5, 200, 34);
        [_toolsView addSubview:_selectedBtn];
        
        //车辆名称
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.frame = CGRectMake(CGRectGetMaxX(_selectedBtn.frame)+10, 5, 100, 34);
        _nameLabel.textColor = COLOR(24, 134, 200, 1);
        _nameLabel.text = @"卡车503#";
        _nameLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        [_toolsView addSubview:_nameLabel];
        
        [_toolsView addSubview:self.promptView];
        self.promptView.frame = CGRectMake(screenWidth-150, 0, 150, 44);
    }
    
    return _toolsView;
}

//A杆 B杆
- (UIView *)promptView{
    
    if (!_promptView) {
        
        _promptView = [[UIView alloc]init];
        
        //w:30
        CGFloat w = 30.0;
        
        UILabel *ALabel = [self crateABLabel:@"A杆"];
        ALabel.frame = CGRectMake(0, 5,  w, 34);
        [_promptView addSubview:ALabel];
        
        UILabel *Alabel_R = [self crateABLabel:@""];
        Alabel_R.frame = CGRectMake(w, 15,  w, 14);
        Alabel_R.backgroundColor = COLOR(24, 134, 200, 1);
        [_promptView addSubview:Alabel_R];
        
        UILabel *BLabel = [self crateABLabel:@"B杆"];
        BLabel.frame = CGRectMake(w*2+10, 5,  w, 34);
        BLabel.textColor = [UIColor greenColor];
        [_promptView addSubview:BLabel];
        
        UILabel *Blabel_R = [self crateABLabel:@""];
        Blabel_R.frame = CGRectMake(w*3+10, 15,  w, 14);
        Blabel_R.backgroundColor = [UIColor greenColor];;
        [_promptView addSubview:Blabel_R];
    }
    
    return _promptView;
}

- (UILabel *)crateABLabel:(NSString *)title{
    
    UILabel *label = [[UILabel alloc]init];
    label.textColor = COLOR(24, 134, 200, 1);
    label.text = title;
    label.font = [UIFont boldSystemFontOfSize:15.0f];
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
}

//曲线选项View
- (UIView *)selectedBackView{
    
    if (!_selectedBackView) {
        
        _selectedBackView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeOptionView)];
        tap.delegate = self;
        [_selectedBackView addGestureRecognizer:tap];
        
        _optionsTableView = [[FCVehicleIolAndCurveOptionTableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _optionsTableView.layer.borderWidth = 1.0f;
        _optionsTableView.layer.borderColor = COLOR(13, 169, 215, 1).CGColor;
        [_selectedBackView addSubview:_optionsTableView];
        
        __weak typeof(self) weakSelf = self;
        
        _optionsTableView.selectedCompletionHandle = ^(NSString *type, NSString *paramID, NSString *paramName){
            
            [weakSelf.selectedBtn setTitle:paramName forState:UIControlStateNormal];
            
            [weakSelf closeOptionView];
            
            weakSelf.lineView.data = nil;
            [weakSelf.lineView removeFromSuperview];
            
            if ([paramName isEqualToString:@"速度曲线"]) {
                
                [weakSelf drawSpeedCurve];
                 weakSelf.promptView.hidden = YES;
            }else {
                [weakSelf drawCarOilCurve];
                weakSelf.promptView.hidden = NO;
                
            }
        };
    }
    
    return _selectedBackView;
}

#pragma mark 打开选项框 关闭选项框
//打开选项框
- (void)showViewWithY:(CGFloat)y andHeight:(CGFloat)h andTag:(NSInteger)tag{
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self.selectedBackView];
    _optionsTableView.frame = CGRectMake(5, y, 200, h);
    _optionsTableView.tag = tag;
}

//关闭选项框
-(void)closeOptionView{
    
    [self.selectedBackView removeFromSuperview];
}

//tap手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//判断如果点击的是tableView的cell，就把手势给关闭了
        
        return NO;//关闭手势
    }
    //否则手势存在
    return YES;
}

- (NSArray *)selectedArray{
    
    if (!_selectedArray) {
        
        _selectedArray = @[@"油耗曲线",@"速度曲线"];
    }
    
    return _selectedArray;
}

#pragma mark - 设置导航视图
- (UIView *)navigationView{
    
    if (!_navigationView) {
        
        _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, kNavigationViewHeight)];
        _navigationView.backgroundColor = COLOR(24, 134, 200, 1);
        
        UIButton *backBtn = [[UIButton alloc]init];
        [backBtn setImage:[UIImage imageNamed:@"leftArrow"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
        backBtn.frame = CGRectMake(10, 17, 70, 30);
        //设置按钮内容水平左对齐
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_navigationView addSubview:backBtn];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.frame = CGRectMake((screenWidth-100)/2, 17, 100, 30);
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = [CommClass getTodayDateStr];
        titleLabel.font = [UIFont systemFontOfSize:18.0f];
        [_navigationView addSubview:titleLabel];
    }
    
    return _navigationView;
}

- (void)backBtnClick{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 懒加载

- (NSMutableArray *)speedCurveListArray{
    
    if (!_speedCurveListArray) {
        
        _speedCurveListArray = [NSMutableArray array];
    }
    
    return _speedCurveListArray;
}

-(NSMutableArray *)carCurveListArray {
    
    if (!_carCurveListArray) {
        _carCurveListArray = [NSMutableArray array];
    }
    return _carCurveListArray;

}
@end
